package com.kabuda.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kabuda.blog.dao.pojo.Article;
import com.kabuda.blog.dao.pojo.SysUser;
import com.kabuda.blog.mapper.ArticleMapper;
import com.kabuda.blog.mapper.UserMapper;
import com.kabuda.blog.service.IUserService;
import com.kabuda.blog.vo.Result;
import com.kabuda.blog.vo.SysUserVo;
import com.qiniu.util.Md5;
import org.apache.commons.codec.digest.Md5Crypt;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author kabuda
 * @create 2023-04-02 17:45
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements IUserService {
    @Autowired
    private ArticleMapper articleMapper;

    public  static  final  String  salt="$1$dabuka";

    @Override
    public List<SysUserVo> listVo() {
        List<SysUser> list = this.list();
        List<SysUserVo> sysUserVos = copyList(list);
        return sysUserVos;
    }

    @Override
    public int changeStatus(Integer status,Long id) {
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        if(status==0){
            status=1;
        }else{
            status=0;
        }
        sysUser.setDeleted(status);
        return this.baseMapper.updateById(sysUser);
    }

    @Override
    public SysUserVo getUserById(Long id) {
        SysUser sysUser = this.baseMapper.selectById(id);
        return   copy(sysUser);
    }

    @Override
    public void addUser(@RequestBody SysUser sysUser) {
        String account = sysUser.getAccount();
        if(getByAccount(account)==null){
            String password = sysUser.getPassword();
            String md5Crypt = Md5Crypt.md5Crypt(password.getBytes(StandardCharsets.UTF_8),salt);
            sysUser.setPassword(md5Crypt);
            sysUser.setCreateDate(System.currentTimeMillis());
            sysUser.setStatus("0");
            sysUser.setDeleted(0);
            sysUser.setAdmin(1);
            sysUser.setAvatar("/static/img/logo.png");
            sysUser.setSalt(salt);
            this.save(sysUser);
        }else{
            throw new RuntimeException("用户已经存在");
        }
    }

    @Override
    public void edit(SysUser sysUser) {
        if(sysUser.getPassword()!=null) {
            String password = sysUser.getPassword();
            sysUser.setPassword(Md5Crypt.md5Crypt(password.getBytes(StandardCharsets.UTF_8), salt));
        }
        this.updateById(sysUser);
    }

    @Override
    public void deleteUser(Long id) {
        //删除用户信息
        this.removeById(id);
        //修改作者id为用户已注销
        LambdaUpdateWrapper<Article> wrapper = new LambdaUpdateWrapper();
        wrapper.eq(Article::getAuthorId,id);
        Article article = new Article();
        //修改为系统预留用户：用户已注销
        article.setAuthorId(2L);
        articleMapper.update(article,wrapper);
    }

    public SysUser getByAccount(String account){
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("account",account);
       return this.baseMapper.selectOne(wrapper);
    }

    public SysUserVo copy(SysUser sysUser) {
        SysUserVo sysUserVo = new SysUserVo();
        BeanUtils.copyProperties(sysUser, sysUserVo);
        Long lastLogin = sysUser.getLastLogin();
        Long createDate = sysUser.getCreateDate();
        sysUserVo.setLastLogin(new DateTime(lastLogin).toString("yyyy-MM-dd HH:mm:ss"));
        sysUserVo.setCreateDate(new DateTime(createDate).toString("yyyy-MM-dd HH:mm:ss"));
        return sysUserVo;
    }

    public List<SysUserVo> copyList(List<SysUser> list) {
        ArrayList<SysUserVo> userVos = new ArrayList<>();
        for (SysUser sysUser : list) {
            SysUserVo copy = copy(sysUser);
            userVos.add(copy);
        }
        return userVos;
    }
}
